<template>
  <div class="app-container">
    <!-- 条件搜索 -->
    <div class="filter-container">
      <el-form ref="queryForm" :model="listQuery.param" size="small" :inline="true" label-width="auto">
        <el-form-item label="流程实例ID" prop="procInstId">
          <el-input v-model="listQuery.param.procInstId" placeholder="流程实例ID" class="filter-item" @keyup.enter.native="handleFilterQuery" />
        </el-form-item>
        <el-form-item label="业务流水号" prop="bizKey">
          <el-input v-model="listQuery.param.bizKey" placeholder="业务流水号" class="filter-item" @keyup.enter.native="handleFilterQuery" />
        </el-form-item>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilterQuery">
          搜索
        </el-button>
        <el-button v-waves class="filter-item" icon="el-icon-refresh-right" @click="handleRestQuery">
          重置
        </el-button>
      </el-form>
    </div>
    <div class="filter-container">
      <div style="float: right;">
        <el-button icon="el-icon-refresh" circle @click="handleFilterQuery" />
      </div>
    </div>

    <!-- 数据表格 -->
    <el-table
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      scrollbar-always-on="true"
    >
      <el-table-column type="selection" width="50px" align="center" />
      <el-table-column label="序号" type="index" width="50px" align="center" />
      <el-table-column label="流程实例名称" min-width="150px" align="left">
        <template v-slot="{row}">
          <span>{{ row.procInstName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务流水号" min-width="150px" align="left">
        <template v-slot="{row}">
          <span>{{ row.bizKey }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前节点名称" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.nodeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="160px" align="left">
        <template v-slot="{row}">
          <span>{{ row.createTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="异常描述" min-width="100px" align="left">-->
      <!--        <template v-slot="{row}">-->
      <!--          <span>{{ row.exceptionMsg }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="流程实例ID" min-width="150px" align="left">
        <template v-slot="{row}">
          <span>{{ row.procInstId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="160px" class-name="small-padding fixed-width">
        <template v-slot="{row,$index}">
          <el-button size="mini" type="info" icon="el-icon-document" @click="handleDetail(row, $index)">详情</el-button>
          <el-button v-permission="permission.deal" size="mini" type="primary" icon="el-icon-thumb" @click="handleDeal(row, $index)">处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表格分页 -->
    <pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="handleInit" />

    <!-- 详情页面 -->
    <el-drawer
      :visible.sync="visible"
      direction="rtl"
      size="50%"
      :close-on-press-escape="false"
      :wrapper-closable="false"
      :show-close="false"
    >
      <template v-slot:title>
        <div class="drawer-header">
          <span>详情</span>
          <div>
            <el-button icon="el-icon-close" @click="handleClose">
              取消
            </el-button>
          </div>
        </div>
      </template>
      <editor :value="trace" :options="editorOptions" lang="java" theme="chrome" width="100%" height="100%" @init="editorInit" />
    </el-drawer>

  </div>
</template>

<script>
import { handlePageList, handleDeal, handleTrace } from './api'
import permission from '@/directive/permission/index' // 权限判断指令
// import checkPermission from '@/utils/permission' // 权限判断函数
import waves from '@/directive/waves' // waves directive
// import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'

export default {
  // 当前页面名称（组件）
  name: 'FlowInstance',
  // 引用组件
  components: {
    Pagination,
    editor: require('vue2-ace-editor')
  },
  directives: { permission, waves },
  filters: {},
  data() {
    return {
      // 列表table的key
      tableKey: 0,
      // 遮罩层
      loading: true,
      // table数据对象
      list: [],
      // 总条数
      total: 0,
      // table条件查询对象
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        // sort: '+id',
        param: {
          procInstId: undefined,
          bizKey: undefined
        }
      },
      // 表单数据对象
      formData: {},
      // 是否显示弹出层
      visible: false,
      // 弹出层状态值（新增和修改共用同一个表单页面）
      state: '',
      // 弹出层标题
      title: {},
      // 权限标识
      permission: {
        deal: ['flow:exception:deal']
      },
      // 表单校验规则
      rules: {},
      // 配置项
      editorOptions: {
        wrap: true, // 换行
        enableLiveAutocompletion: true, // 智能补全
        enableSnippets: true, // 启用代码段
        readOnly: true
      },
      // 展示的错误信息
      trace: ''
    }
  },
  computed: {},
  watch: {},
  created() {
    this.handleInit()
  },
  mounted() {},
  methods: {
    // 查询table数据对象
    handleInit() {
      this.loading = true
      handlePageList(this.listQuery).then(res => {
        this.list = res.data
        if (res.total) {
          this.total = Number(res.total)
        }
        this.loading = false
      })
    },
    // 条件查询table数据
    handleFilterQuery() {
      this.listQuery.pageNum = 1
      this.handleInit()
    },
    // 重置table查询条件
    handleRestQuery() {
      this.$refs.queryForm.resetFields()
    },
    // 初始化编辑器
    editorInit() {
      require('brace/ext/language_tools')
      require('brace/mode/html')
      require('brace/mode/javascript')
      require('brace/mode/less')
      require('brace/mode/java')
      require('brace/theme/chrome')
      require('brace/snippets/javascript')
      require('brace/snippets/java')
    },
    // 详情
    handleDetail(row) {
      handleTrace({ jobId: row.jobId }).then((res) => {
        this.trace = res.data
      })
      this.visible = true
    },
    // 处理异常
    handleDeal(row, index) {
      this.$confirm('确认处理当前异常吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        handleDeal({ executionId: row.executionId }).then((res) => {
          //  刷新table
          this.handleFilterQuery()
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'success',
            duration: 4000
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    // 关闭弹出层
    handleClose() {
      this.visible = false
      this.trace = ''
    }
  }
}
</script>
